﻿using ImageCalibModule;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GluePathModule
{
    public static class GluePathManager
    {
        public static GluePathCenter gluePathCenter;
        public static double dZoffSet;
        internal static List<GluePathItemBase> copyListContinueItem;
        public static GluePathGroup copyGluePathGroup;
        public static void Init()
        {
            gluePathCenter = GluePathCenter.Load(true);
            FrameManager.FrameManager.saveProject += FrameManager_saveProject;
            FrameManager.FrameManager.saveAsProject += FrameManager_saveAsProject;
            FrameManager.FrameManager.openProject += FrameManager_openProject;
            if (gluePathCenter.listGluePathGroup.Count == 0)
            {
                GluePathGroup gluePathGroup = new GluePathGroup();
                gluePathGroup.Name = "Main";
                gluePathGroup.strRemark = "胶路设定";
                GluePathUnit gluePathUnit = new GluePathUnit();
                gluePathUnit.drawMode = DrawMode.UnitIsSelected;
                gluePathUnit.Name = "Path1";
                gluePathGroup.listGlueUnit.Add(gluePathUnit);
                gluePathCenter.listGluePathGroup.Add(gluePathGroup);
            }

        }
        private static bool FrameManager_openProject()
        {
            gluePathCenter = GluePathCenter.Load(true);
            return true;
        }

        private static bool FrameManager_saveAsProject()
        {
            Save();
            return true;
        }

        private static bool FrameManager_saveProject()
        {
            Save();
            return true;
        }
        public static bool Save()
        {
            gluePathCenter.Save(true);
            return true;
        }
        public static GluePathGroup GetPathGroup(string Name)
        {
            string strGluePathName = Name;
            for (int i = 0; i < gluePathCenter.listGluePathGroup.Count; i++)
            {
                if (gluePathCenter.listGluePathGroup[i].Name == strGluePathName)
                {
                    return gluePathCenter.listGluePathGroup[i];
                }
            }
            return null;
        }

        public static Form ShowManagerForm()
        {
            
            FormGluePathUnitSetting frmManager = new FormGluePathUnitSetting(gluePathCenter.listGluePathGroup[0]);
            frmManager.TopLevel = false;
            return frmManager;
        }
        public static List<MotionItemBase> GetMotionList(string strGroupName,double dOffSetZ)
        {
            GluePathGroup gluePathGroup = null; ;
            for (int i = 0; i < gluePathCenter.listGluePathGroup.Count; i++)
            {
                if (gluePathCenter.listGluePathGroup[i].Name == strGroupName)
                {
                    gluePathGroup = gluePathCenter.listGluePathGroup[i];
                }
            }
            if (gluePathGroup == null)
                return null;
            else
                return gluePathGroup.GetMotionList(dOffSetZ);
        }
        
        public static void SetGroupUnitCurrentCenter(string strGroupName,string strUnitName,double dLocalX, double dLocalY, double dLocalU)
        {
            GluePathGroup gluePathGroup = null; ;
            for (int i = 0; i < gluePathCenter.listGluePathGroup.Count; i++)
            {
                if (gluePathCenter.listGluePathGroup[i].Name == strGroupName)
                {
                    gluePathGroup = gluePathCenter.listGluePathGroup[i];
                    break;
                }
            }
            if (gluePathGroup == null)
                return;
            GluePathUnit currentUnit = null;
            for (int i = 0; i < gluePathGroup.listGlueUnit.Count; i++)
            {
                if (gluePathGroup.listGlueUnit[i].Name == strUnitName)
                {
                    currentUnit = gluePathGroup.listGlueUnit[i];
                    break;
                }
            }
            if (currentUnit == null)
                return;

            currentUnit.CurrentLocalX = dLocalX;
            currentUnit.CurrentLocalY = dLocalY;
            currentUnit.CurrentLocalU = dLocalU;
        }
        public static void SetGroupUnitCurrentTool(string strGroupName, string strUnitName, double dToolX, double dToolY)
        {
            GluePathGroup gluePathGroup = null; ;
            for (int i = 0; i < gluePathCenter.listGluePathGroup.Count; i++)
            {
                if (gluePathCenter.listGluePathGroup[i].Name == strGroupName)
                {
                    gluePathGroup = gluePathCenter.listGluePathGroup[i];
                    break;
                }
            }
            if (gluePathGroup == null)
                return;
            GluePathUnit currentUnit = null;
            for (int i = 0; i < gluePathGroup.listGlueUnit.Count; i++)
            {
                if (gluePathGroup.listGlueUnit[i].Name == strUnitName)
                {
                    currentUnit = gluePathGroup.listGlueUnit[i];
                    break;
                }
            }
            if (currentUnit == null)
                return;

            currentUnit.CurrentToolX = dToolX;
            currentUnit.CurrentToolY = dToolY;
        }
    }
}
